package com.lbs.jiepiaisi.parser;
import android.util.Log;

import com.google.gson.Gson;
import com.lbs.jiepiaisi.bean.LoginFailBean;
import com.lbs.jiepiaisi.bean.UserEntity;
import com.lbs.jiepiaisi.bean.VinList;

import org.json.JSONObject;

/**
 *
 * 时间：2017/8/7 17:28
 * 作用：登录解析类（账号登录）
 */

public class LoginAccountParser {

    //解析方法
    public static UserEntity parser(String jsonString){
        UserEntity userEntity=new UserEntity();
        try{
            JSONObject jsonObject=new JSONObject(jsonString);
            int code=jsonObject.getInt("status");
            //成功
            if(code==0){
                String result=jsonObject.getString("result");
                JSONObject jsonObjectResult=new JSONObject(result);
                int uiserid=jsonObjectResult.getInt("userId");
                if(jsonObjectResult.getString("efDepartment") != null && !jsonObjectResult.getString("efDepartment").equals("null")){
                    String efDepartment=jsonObjectResult.getString("efDepartment");
                    JSONObject jsonObjectEfDepartment=new JSONObject(efDepartment);
                    int deptId=jsonObjectEfDepartment.getInt("deptId");
                    String deptName = jsonObjectEfDepartment.getString("deptName");
                    String parentDeptId = jsonObjectEfDepartment.getString("parentDeptId");
                    userEntity.setDeptId(deptId);
                    userEntity.setUserId(uiserid);
                    userEntity.setDeptName(deptName);
                    userEntity.setParentDeptId(parentDeptId);
                }
                userEntity.setCode(code);
            }else{//失败
                userEntity.setCode(code);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  userEntity;
    }
    //车架号登录解析方法
    public static UserEntity parserCar(String jsonString){
        UserEntity userEntity=new UserEntity();
        try{
            JSONObject jsonObject=new JSONObject(jsonString);
            int code=jsonObject.getInt("status");
            //成功
            if(code==0){
                String result=jsonObject.getString("result");
                JSONObject jsonObjectResult=new JSONObject(result);
                String ownerName=jsonObjectResult.getString("ownerName");

                Double lat=jsonObjectResult.getJSONObject("point").getDouble("lat");
                Double lng=jsonObjectResult.getJSONObject("point").getDouble("lng");
                float dir = (float) jsonObjectResult.getJSONObject("point").getDouble("dir");
                UserEntity.Point point = new UserEntity.Point();
                point.setLat(lat);
                point.setLng(lng);
                point.setDir(dir);
                userEntity.setPoint(point);
                userEntity.setOwnerName(ownerName);
                userEntity.setEqNo(jsonObjectResult.getString("eqNo"));
                userEntity.setDeptId(jsonObjectResult.getInt("egId"));
                userEntity.setStatus(jsonObjectResult.getString("status"));

            }else{//失败
                userEntity.setCode(code);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  userEntity;
    }

    //解析方法
    public static VinList parserVin(String jsonString){
        return  new Gson().fromJson(jsonString,VinList.class);
    }

    public static LoginFailBean parserLoginFail(String jsonString) {
        return  new Gson().fromJson(jsonString,LoginFailBean.class);
    }
}
